//
//  YMStatusCell.swift
//  YMWeiBo
//
//  Created by mimi on 16/7/24.
//  Copyright © 2016年 mimi. All rights reserved.
//

import UIKit
import SnapKit

class YMStatusCell: UITableViewCell {
    //约束记录底部视图的约束
    var toolBarBottomConstraint:Constraint?
    //定义一个Viewmodel
    var statusViewModel:YMStatusViewModel?{
        
        didSet{
            originalView.statusViewModel = statusViewModel

            //没转发卸载底部约束,重新赋值
            self.toolBarBottomConstraint?.uninstall()
            //没有转发微博
            if statusViewModel?.status?.retweeted_status == nil{
               
                toolBarView.snp_updateConstraints(closure: { (make) in
                    
                    self.toolBarBottomConstraint = make.top.equalTo(originalView.snp_bottom).constraint
                })
                
                reweetView.hidden = true
                
            }else{
                
                reweetView.statusViewModel = statusViewModel
                //有转发微博
                toolBarView.snp_updateConstraints(closure: { (make) in
                    self.toolBarBottomConstraint = make.top.equalTo(reweetView.snp_bottom).constraint
                })
                reweetView.hidden = false
            }
            toolBarView.statusViewModel = statusViewModel
        }
    }
//    var statusViewModel:YMStatusViewModel?

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //设置界面
    func setupUI() {
       //添加控件
        contentView.addSubview(originalView)
        contentView.addSubview(reweetView)
        contentView.addSubview(toolBarView)
        //设置约束
        
        originalView.snp_makeConstraints { (make) in
            make.top.trailing.leading.equalTo(contentView)
            //设置虚拟高度
//            make.height.equalTo(50)
        }

        reweetView.snp_makeConstraints { (make) in
            make.leading.trailing.equalTo(contentView)
            make.top.equalTo(originalView.snp_bottom)
            //设置虚拟高度
//            make.height.equalTo(50)
        }
        
        toolBarView.snp_makeConstraints { (make) in
            make.leading.trailing.equalTo(contentView)
            make.height.equalTo(35)
           self.toolBarBottomConstraint = make.top.equalTo(reweetView).constraint
            
        }
        
        contentView.snp_makeConstraints { (make) in
            make.leading.trailing.top.equalTo(self)
            make.bottom.equalTo(toolBarView)
        }
    }
    
    
    
    
   //        MARK: - 懒加载
    //原创微博
    private lazy var originalView:YMStatusOriginalView = YMStatusOriginalView()
    //转发微博
    private lazy var reweetView:YMReweetView = YMReweetView()
    //底部视图
    private lazy var toolBarView: YMStatusToolsBarView = YMStatusToolsBarView()

}














